/**
 * 每个view 都要有一个 _id
 *   有一个parentView 和 一个 rootEl
 */
class BaseView {

    constructor() {
        this._id = window.myTools.genId();
        this._rootEl = null;
        this._parentView = null;
    }

    get id() {
        return this._id;
    }

    get rootEl() {
        return this._rootEl;
    }


    set rootEl(newEl) {
        this._rootEl = newEl;
    }

    addView(view) {
        if (!view) {
            throw new Error(`view is null!`);
        }

        if (!this._rootEl) {
            throw new Error(`rootEl is null!`);
        }

        if (view instanceof BaseView) {
            this._rootEl.appendChild(view._rootEl);
            view._parentView = this;
        }else{
            throw new Error(`view:${view} is not baseView!`);
        }
    }

    set parentView(view){
        if (!view) {
            throw new Error(`view is null!`);
        }
        if(view instanceof BaseView){
            this._parentView = view;
        }else{
            throw new Error(`view:${view} is not baseView!`);
        }
    }

    get parentView(){
        return this._parentView
    }

    destroy() {
        if (!this._rootEl) {
            throw new Error(`rootEl is null!`);
        }
        if (!this._rootEl.parentNode) {
            //throw new Error(`this._rootEl.parentNode is null!`);
            return;
        }
        this._rootEl.parentNode.removeChild(this._rootEl);
    }
}

module.exports = BaseView;
